//
//  GLBottomLineTextField.m
//  GLLibDemo
//
//  Created by huang long on 17/4/26.
//  Copyright © 2017年 历山大亚. All rights reserved.
//

#import "GLBottomLineTextField.h"

#import "NSString+GL.h"
@implementation GLBottomTextField

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    NSString *actStr = NSStringFromSelector(action);
    if (_performActions.count > 0) {
        if ([_performActions containsObject:actStr]) {
            return YES;
        }else{
            return NO;
        }
    }else if (_unperformActions > 0){
        if ([_unperformActions containsObject:actStr]) {
            return NO;
        }else{
            return YES;
        }
    }else{
        return [super canPerformAction:action withSender:sender];
    }
}


-(void)setPerformActions:(NSArray *)performActions{
    _performActions = performActions;
    _unperformActions = nil;
}

- (void)setUnperformActions:(NSArray *)unperformActions{
    _unperformActions = unperformActions;
    _performActions = nil;
}

@end

@interface GLBottomLineTextField ()<UITextFieldDelegate>

@property(nonatomic , weak)UIView * contentView;

@end

@implementation GLBottomLineTextField

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *view = [[UIView alloc] init];
        [self addSubview:view];
        _contentView = view;
        
        GLBottomTextField *field = [[GLBottomTextField alloc] initWithFrame:CGRectZero];
        field.font = [UIFont systemFontOfSize:14];
        field.delegate = self;
        [view addSubview:field];
        _textField = field;
        
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = @"#cccccc".toColor;
        [view addSubview:line];
        _lineView = line;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:field];
    }
    return self;
}

- (void)layoutSubviews{
//    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_offset(_contentInset);
//    }];
//    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_offset(_textFieldInset);
//    }];
//    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.with.bottom.offset(0);
//        make.height.mas_equalTo(1);
//    }];
    
    [self loadContentEdges];
    [self loadTextFieldEdges];
    [self loadBottomViewEdges];
    
    [super layoutSubviews];
}

- (void)loadContentEdges{
    UIView *contentView = self.contentView;
    
    NSDictionary *metrics = @{@"left":@(self.contentInset.left),
                              @"top":@(self.contentInset.top),
                              @"right":@(self.contentInset.right),
                              @"bottom":@(self.contentInset.bottom),
                              };
    
    NSDictionary *dict2 = NSDictionaryOfVariableBindings(contentView);
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *array3 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[contentView]-right-|" options:0 metrics:metrics views:dict2];
    NSArray *array4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[contentView]-bottom-|" options:0 metrics:metrics views:dict2];
    [self addConstraints:array3];
    [self addConstraints:array4];
}

- (void)loadTextFieldEdges{
    UIView *textField = self.textField;
    
    NSDictionary *metrics = @{@"left":@(self.textFieldInset.left),
                              @"top":@(self.textFieldInset.top),
                              @"right":@(self.textFieldInset.right),
                              @"bottom":@(self.textFieldInset.bottom),
                              };
    
    NSDictionary *dict2 = NSDictionaryOfVariableBindings(textField);
    textField.translatesAutoresizingMaskIntoConstraints = NO;
    NSArray *array3 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[textField]-right-|" options:0 metrics:metrics views:dict2];
    NSArray *array4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[textField]-bottom-|" options:0 metrics:metrics views:dict2];
    [self addConstraints:array3];
    [self addConstraints:array4];
}


- (void)loadBottomViewEdges{
    UIView *lineView = self.lineView;
    
    NSDictionary *dict2 = NSDictionaryOfVariableBindings(lineView);
    lineView.translatesAutoresizingMaskIntoConstraints = NO;
    
    //间距不填写时 默认为8
    
    NSArray *array3 = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[lineView]-0-|" options:(0) metrics:nil views:dict2];
    NSArray *array4 = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[lineView(==1)]-1-|" options:0 metrics:nil views:dict2];
    [self addConstraints:array3];
    [self addConstraints:array4];
}

#pragma mark - notification
- (void)textChange:(NSNotification *)notification{
    if (notification.object == _textField) {
        if (self.maxByteLength > 0) {
            if ([_textField.text byteLength] > _maxByteLength) {
                
                _textField.text = [_textField.text substringToIndex:[_textField.text indexWithMaxByteLength:self.maxByteLength]];
            }
        }
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldBeginEditing:)]) {
        return [self.delegate textFieldShouldBeginEditing:textField];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _lineView.backgroundColor = @"#1567f0".toColor;
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [self.delegate textFieldDidBeginEditing:textField];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    _lineView.backgroundColor = @"#cccccc".toColor;
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
        [self.delegate textFieldDidEndEditing:textField];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldEndEditing:)]) {
        return [self.delegate textFieldShouldEndEditing:textField];
    }
    
    return YES;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (self.limitEmoji) {
        if ([[textField.textInputMode primaryLanguage] isEqualToString:@"emoji"]) {
            return NO;
        }
    }
    _lineView.backgroundColor = @"#1567f0".toColor;
    if (self.delegate && [self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [self.delegate textField:textField shouldChangeCharactersInRange:range replacementString:string];
    }
    if ([string isEqualToString:@""]) {
        return YES;
    }else{
        if (self.maxByteLength > 0) {
            if ([textField.text byteLength] < _maxByteLength) {
                return YES;
            }else{
                return NO;
            }
        }
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        return [self.delegate textFieldShouldClear:textField];
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldShouldReturn:)]) {
        return [self.delegate textFieldShouldReturn:textField];
    }
    return YES;
}


#pragma mark - getter setter
- (void)setText:(NSString *)text{
    self.textField.text = text;
}

- (NSString *)text{
    return self.textField.text;
}

@end
